package ui;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

import midlet.VadeMecumMIDlet;
import multimedia.SaveSnapshotThread;
import multimedia.SendToServiceWithContextThread;
import multimedia.SendToServiceWithoutContextThread;
import multimedia.ThreadListenAudio;
import multimedia.ThreadRecordAudio;

public class RecordAudioForm extends Form implements CommandListener {
	
	private ThreadRecordAudio threadRecordAudio;	
	private ThreadListenAudio threadListenAudio;
	private Command cmdHear;
	private Command cmdFinishRecording;
	private Command cmdRecord;
	private Command cmdCancel;
	public Command save = null;
	public Command sendToServiceWhithoutContext = null;
	public Command sendToServiceWithContext = null;
	public VadeMecumMIDlet midlet;
	private StringItem mensagens;
	private Displayable previousForm;
	private Timer timer;
	
	public RecordAudioForm(VadeMecumMIDlet midlet, Displayable previousForm) {
		super("Audio recording");
		this.midlet    	  = midlet;
		this.previousForm = previousForm;
		
		this.cmdRecord 	  = new Command("Init record", Command.OK, 0);
		this.cmdCancel 	  = new Command("Back", Command.CANCEL, 0);
		save = new Command("Save audio", Command.OK, 1);
		sendToServiceWhithoutContext = new Command("Send without context", Command.OK, 1);
		sendToServiceWithContext = new Command("Send with context", Command.OK, 1);
		
		this.mensagens 	  = new StringItem("   Ready to record...", "");
		
		this.append(this.mensagens);
		this.addCommand(this.cmdRecord);
		this.addCommand(this.cmdCancel);
		this.setCommandListener(this);
	}
	
	public ByteArrayOutputStream getOutputAudio() {
		return this.threadRecordAudio.getOutputAudio();
	}
	
	public void playSound() {
		this.mensagens.setLabel("   Playing audio...");
		this.threadListenAudio = new ThreadListenAudio(this.threadRecordAudio.getOutputAudio().toByteArray(), this);
		new Thread(this.threadListenAudio).start();
	}
	
	public void beginRecordTasks() {
		this.removeCommand(this.cmdRecord);
		try {
			this.removeCommand(this.save);
			this.removeCommand(this.sendToServiceWhithoutContext);
			this.removeCommand(sendToServiceWithContext);
		} catch (Exception e) {
			this.midlet.error(e);
		}
		this.cmdFinishRecording = new Command("Done", Command.OK, 0);
		this.addCommand(this.cmdFinishRecording);
		this.mensagens.setLabel("   Recording...");
//		this.timer = new Timer();
//		this.timer.schedule(new RecordingTime(), 6000);		
	}

	public void stopRecording() {
		try {
			if(this.cmdHear == null) {
				this.cmdHear = new Command("Listen", Command.SCREEN, 1);
				this.addCommand(this.cmdHear);
			}
			this.addCommand(save);
			this.addCommand(sendToServiceWhithoutContext);
			this.addCommand(sendToServiceWithContext);
			
			this.removeCommand(this.cmdFinishRecording);
			this.threadRecordAudio.getRecordControl().commit();
			this.threadRecordAudio.getPlayerAudio().close();
			this.mensagens.setLabel("   Audio recorded!");
		} catch (IOException ioe) {
			midlet.error(ioe);
		}

	}
	
	public void commandAction(Command cmd, Displayable _displayable) {
		if(cmd == this.cmdRecord) {
			this.threadRecordAudio = new ThreadRecordAudio(this);
			new Thread(this.threadRecordAudio).start();
			this.beginRecordTasks();
		} else if(cmd == this.cmdFinishRecording) {
			this.stopRecording();
		} else if(this.cmdHear == cmd) {
			this.playSound();
		} else if (this.save == cmd) {
			saveSnapshot();
		} else if (this.sendToServiceWhithoutContext == cmd) {
			sendWithoutContext();
		} else if (this.sendToServiceWithContext == cmd) {
			sendWithContext();
		} else if(this.cmdCancel == cmd) {
			midlet.display.setCurrent(this.previousForm);
		}
	}
	
	public void saveSnapshot() {
		Thread saveThread = new Thread(new SaveSnapshotThread(midlet, this, this.threadRecordAudio.getOutputAudio().toByteArray(), System.currentTimeMillis()));
		saveThread.start();
	}

	public void sendWithoutContext() {
		Thread sendThread = new Thread(new SendToServiceWithoutContextThread(midlet, this, this.threadRecordAudio.getOutputAudio().toByteArray(), System.currentTimeMillis()));
		sendThread.start();
	}

	public void sendWithContext() {
		Thread sendThread = new Thread(new SendToServiceWithContextThread(midlet, this, this.threadRecordAudio.getOutputAudio().toByteArray(), System.currentTimeMillis()));
		sendThread.start();
	}
	
	private class RecordingTime extends TimerTask {

		public void run() {
//			stopRecording();		
		}
		
	}
	
}
